package engine.newUi
{
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.ObjectData;
	import engine.data.RoleVo;
	import engine.data.View;
	import engine.display.newAttack.vo.FriendVo;
	import engine.event.SceneEvent;
	import engine.event.SoundEvent;
	import engine.logic.LogSendLogic;
	import engine.logic.McPool;
	import engine.net.datatype.WString;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	
	import newx.com.ResManager;
	import newx.com.control.NxClip;

	public class BottomIndexLogic
	{
		private const TXT_INFO:String="text";
		private const DATA:String ="data";
		private const CION:String ="cion";
		private var _ui			:BottomIndexUI;
		/**人物按钮*/
		private var _roleBtn		:SimpleButton;
		/**背包按钮*/
		private var _packBtn		:SimpleButton;	
		/**阵法按钮*/
		private var _statusBtn		:SimpleButton;
		/**拜师按钮*/
		private var _emulateBtn	:SimpleButton;
		/**布阵按钮*/
		private var _embattleBtn	:SimpleButton;		
		/**经脉按钮*/
		private var _pulseBtn		:SimpleButton;
		/**
		 *武学 
		 */		
		private var _kungfuBtn:SimpleButton
		/**任务按钮*/
		private var _taskBtn		:SimpleButton;		
		/**好友按钮*/
		public var _buddyBtn		:SimpleButton;		
		/**修练按钮*/
		private var _addExpBtn		:SimpleButton;		
		/**帮派按钮*/
		private var _factionBtn	:SimpleButton;		
		/**镶嵌按钮*/
		private var _inlayBtn		:SimpleButton;		
		/**强化按钮*/
		private var _epurateBtn	:SimpleButton;
		/**洗髓*/
		private var _refreshProp	:SimpleButton;
		/**御兽**/
		private var _petBtn		:SimpleButton;
		/**绝学**/
		private var _specialKungfuBtn:SimpleButton;
		
		private var _epurateBtn22	:SimpleButton;
		/**好友消息特效**/
		private var _friendtxMC	:MovieClip;
		
		private var _expTxt		:TextField;
		
		private var _expLine		:MovieClip;
		
		private var _expLineWidth	:int;
		
		private var _model			:ModelLocator;
		
		private var _role			:RoleVo;
		
		private var _btnList		:Array;
		
		private var _bindManage	:BindManage;
		
		private var _btnEffectMap:Object={};
		private static var _self:BottomIndexLogic;
		
		private var _isShow			:Boolean	=	true;
		public static function getInstance():BottomIndexLogic
		{
			if(_self == null)
			{
				_self	=	new BottomIndexLogic();
			}
			return _self;
		}
		
		public function BottomIndexLogic()
		{
		}
		
		public function init():void
		{
			_model		= ModelLocator.getInstance();
			
			_role		= _model.scene.Role;
			
			_bindManage	= BindManage.getInstance();
			
			_ui			= BottomIndexUI.getInstance();
			
			_roleBtn	= _ui.bottomBg.getChildByName( "btn_0" ) as SimpleButton;
			
			_packBtn	= _ui.bottomBg.getChildByName( "btn_1" ) as SimpleButton;
			
			_embattleBtn= _ui.bottomBg.getChildByName( "btn_2" ) as SimpleButton;
			
			_pulseBtn	= _ui.bottomBg.getChildByName( "btn_3" ) as SimpleButton;
			
			_petBtn		= _ui.bottomBg.getChildByName( "btn_4") as SimpleButton;
			
			_buddyBtn	= _ui.bottomBg.getChildByName( "btn_5" ) as SimpleButton;
			
			_addExpBtn	= _ui.bottomBg.getChildByName( "btn_6" ) as SimpleButton;
			
			_factionBtn	= _ui.bottomBg.getChildByName( "btn_7" ) as SimpleButton;
			
			_refreshProp	= _ui.bottomBg.getChildByName( "btn_8" ) as SimpleButton;
			
			_epurateBtn	= _ui.bottomBg.getChildByName( "btn_9" ) as SimpleButton;
			
			_kungfuBtn= _ui.bottomBg.getChildByName( "btn_11" ) as SimpleButton;
			
			_specialKungfuBtn= _ui.bottomBg.getChildByName( "btn_12" ) as SimpleButton;
			
			_statusBtn	= _ui.bottomBg.getChildByName( "btn_10" ) as SimpleButton;	
			
			_friendtxMC =_ui.bottomBg.getChildByName("btn_20") as MovieClip;
	
			_btnList	= [ _roleBtn, _packBtn, _embattleBtn, _pulseBtn, _petBtn,_taskBtn, 
				_buddyBtn, _addExpBtn, _factionBtn,_epurateBtn,_emulateBtn,_statusBtn,_kungfuBtn, _refreshProp,_specialKungfuBtn];
			
			_expTxt		= _ui.bottomBg.getChildByName( "expTxt" ) as TextField;
			_expTxt.text = "";
			_expLine	= _ui.bottomBg.getChildByName( "exp" ) as MovieClip;
			
			_expLineWidth = _expLine.width;
			_friendtxMC.visible=false;
			
			_model.addEventListener( SceneEvent.SceneLoaded	,onSceneLoaded );
//			a=new GlowTween();
		}
		
		private function onSceneLoaded(e:SceneEvent):void
		{
			_model.removeEventListener(SceneEvent.SceneLoaded	, onSceneLoaded );
			
			bind();
			
			addListener();
			
			_expTxt.addEventListener( MouseEvent.MOUSE_OVER, onShowTips );
			_expTxt.addEventListener( MouseEvent.MOUSE_OUT, onHideTips );
			_friendtxMC.mouseEnabled =_friendtxMC.mouseChildren =false;
			setFriendTxMcPoint();
		}
		public function setFriendTxMcPoint():void
		{
			_friendtxMC.x=_buddyBtn.x;
			_friendtxMC.y=_buddyBtn.y;
		}
		private function onShowTips(evt:MouseEvent):void
		{
			_expTxt.text = _role.getPlayerExp() + "/" +  _role.getPlayerMaxExp() 
				+ " ( " + int( _role.getPlayerExp() / _role.getPlayerMaxExp() * 10000 ) / 100 + "% ) ";
			
			if( _role.getPlayerExp() > _role.getPlayerMaxExp() )
			{
				_expTxt.text = _role.getPlayerExp() + "/" +  _role.getPlayerMaxExp() + " ( 100% ) ";
			}
//			trace(evt.target.name)
		}
		
		private function onHideTips(evt:MouseEvent):void
		{
			_expTxt.text = "";
		}
	
		private function checkPostion():Boolean
		{
			 var pos:int=_model.scene.Role.GetProperties("GuildPosition");
			 return (pos==1	||pos==2);
		}
		private function bind():void
		{
			_bindManage.BindProp( _role, "PlayerExp", this, "changePlayerExp" );
			_bindManage.BindProp( _role, "PlayerUpgradeExp", this, "changePlayerExp" );
			_bindManage.BindProp(_model.scene.GetView(View.VIEWPORT_TOOL),"BaseCap",this,"lock")
			_bindManage.BindView(_model.scene,View.VIEWPORT_TOOL,packCheck,packRemove);
			_model			.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_SHOW_APPLY_INFO),onShowShengheFamily,false,1);
			if(checkPostion())
			{
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_GET_APPLY_INFO);
			}
			function packRemove(PropertiesName:String,PropertiesValue:ObjectData,data:*):void
			{
				hideFullArrow(_packBtn);
			}
		}
		private function onShowShengheFamily(e:CustomMsgEvent):void
		{
			var num:int=e.pList[0];
			if(num>0	&&	checkPostion())
			{
				showFullArrow(_factionBtn,"custSkin_231")
			}
			else
			{
				hideFullArrow(_factionBtn)
			}
		}
		private function packCheck(PropertiesName:String,PropertiesValue:ObjectData,data:*):void
		{
			if(PackLogic.getInstance().isPackFull)
			{
				showFullArrow(_packBtn);
			}
		}
		private var _lock:int;
		public function set lock(value:int):void
		{
			if(value>_lock&&_lock!=0)
			{
				hideFullArrow(_packBtn);
			}
			_lock=value
		}
		private function showFullArrow(btn:SimpleButton,skin:String="custSkin_230"):void
		{
			if(!_btnEffectMap.hasOwnProperty(btn.name))
			{
				var mc:MovieClip= McPool.borrowRes(skin);
				if(mc)
				{
					if(btn.parent&&btn.visible)
					{
						var obj:Object={arrow:mc,refresh:function():void{	
							mc.x=(btn.x+(btn.width-mc.width)/2)+5;
							mc.y=(btn.y-mc.height);
						}};
						_btnEffectMap[btn.name]=obj;
						obj["refresh"]();
						btn.parent.addChild(mc);
						mc.play();
					}
				}
			}
		}
		public function set needRefreshArrow(b:Boolean):void
		{
			if(b)
			{
				_ui.addEventListener(Event.ENTER_FRAME,refreshBtnArrow);
			}
			else
			{
				_ui.removeEventListener(Event.ENTER_FRAME,refreshBtnArrow);
			}
			refreshBtnArrow();
		}
		private function refreshBtnArrow(e:Event=null):void
		{
			for each (var i:Object in _btnEffectMap) 
			{
				i["refresh"]();
			}
		}
		private function hideFullArrow(btn:SimpleButton):void
		{
			if(checkBtnState(btn.name))
			{
				var mc:MovieClip=_btnEffectMap[btn.name]["arrow"];
				if(mc)
				{
					if(mc.parent)
						mc.parent.removeChild(mc)
					mc.stop();
					delete _btnEffectMap[btn.name]
					McPool.returnRes(mc["url"],mc);
				}
			}
		}
		public function checkBtnState(btnName:String):Boolean
		{
			return _btnEffectMap.hasOwnProperty(btnName);
		}
		private function addListener():void
		{
			for(var i : int = 0; i < _btnList.length; i++)
			{
				if( _btnList[ i ])
				addTipsListener( _btnList[ i ] );
			}
		}
		
		public function get petBtnState():Boolean
		{
			return _petBtn.visible?true:false;
		}
		private function addTipsListener( target : DisplayObject ):void
		{
			target.addEventListener( MouseEvent.CLICK, onClick );
			target.addEventListener( MouseEvent.MOUSE_OVER, onMouseOver );
			target.addEventListener( MouseEvent.MOUSE_OUT, onMouseOut ); 
		}
		
		private function onMouseOver(e:MouseEvent):void
		{
			_model.dispatchEvent(new SoundEvent(SoundEvent.MOUSE_OVER));
		}
		
		private function onMouseOut(e:MouseEvent):void
		{
			_model.dispatchEvent(new SoundEvent(SoundEvent.MOUSE_OUT));
		}
		public function set FriendTxBtn(val:Boolean):void
		{
			setFriendTxMcPoint();
			if(_buddyBtn.visible)
			{
				_friendtxMC.visible=val;
			}
			
		}
		
		public function get FriendTxBtn():Boolean
		{
			return _friendtxMC.visible;
		}
		public function isOK(bool:Boolean):void		
		{
			_isShow	=	bool;
		}
		private function onClick( evt : MouseEvent ):void
		{
			switch( evt.target )
			{
				case _roleBtn:
				{
					
//					StoryMoveUI.getInstance().showTalkInfo("Story_500001");
					MyPropertyLogic.getInstance().ui.showOrHide();
//					EngageFriendLogic.getInstance().callForShow();
					//AlertNextUI.getInstance("XXXXXX","00000",isOK,_isShow);
					LogSendLogic.getInstance().btnClickNumSend(22101);
					
					break;
				}
				case _packBtn:
				{
					PackLogic.getInstance().ui.showOrHide();
					LogSendLogic.getInstance().btnClickNumSend(22102);
					break;
				}
				case _embattleBtn:
				{
					EmbattleLogic.getInstance().ui.showOrHide();
					LogSendLogic.getInstance().btnClickNumSend(22104);
					break;
				}
				case _petBtn:
				{
					
					PetUI.getInstance().showOrHide();
					break;
				}
				case _pulseBtn:
				{
					PulseLogic.getInstance().ui.showOrHide();
					LogSendLogic.getInstance().btnClickNumSend(22105);
					break;
				}
				case _specialKungfuBtn:
				{
					SpecialKungFuUI.getInstance().showOrHide();
					break;
				}
				case _kungfuBtn:
				{
					KungfuPackLogic.getInstance().ui.showOrHide();
					LogSendLogic.getInstance().btnClickNumSend(22109);
					break;
				}
				case _refreshProp:
				{
					WashMarrowLogic.getInstance().ui.showOrHide();
					break;
				}
				case _buddyBtn:
				{
					if(!WhisperLogic.getInstance().whisperUi.created)
					{
						FriendUI.getInstance().showOrHide();
					}
					else
					{
						if(!FriendUI.getInstance().created)
							FriendUI.getInstance().showOrHide();
					}
					LogSendLogic.getInstance().btnClickNumSend(22107);
					WhisperLogic.getInstance().checkHaveMsg();

					break;
				}
				case _addExpBtn:
				{
					if( SitCrossleggedLogic.getInstance().isSit )
					{
						SitCrossleggedLogic.getInstance().stopSit();
					}else
					{
						SitCrossleggedLogic.getInstance().startSit();
						LogSendLogic.getInstance().btnClickNumSend(22110);
					}
					
					break;
				}
				case _factionBtn:
				{
					if( FamilyInfoUI.getInstance().visible )
					{
						FamilyInfoUI.getInstance().hide();
					}else
					{
						var flagT	:String	=	_model.scene.Role.GetProperties("GuildName");
						if(0 < flagT.length)
						{
							//查看公会详细信息
							
							SystemSender.getInstance().CustomMsg(
								CustomToServerFlag.CLIENT_CUSTOMMSG_GET_GUILD_INFO ,new WString(flagT));
						}
						else
						{
							//获得所有家族信息列表
							//						UIManage.getInstance().requestOpenUI(FamilyLogic.getInstance().ui);
							FamilyLogic.getInstance().ui.showOrHide();
							
						}
						//AlchemyUI.getInstance().show();
					
					}
					
					LogSendLogic.getInstance().btnClickNumSend(22108);
					break;
				}
				case _epurateBtn:
				{
					EquipModifiedUI.getInstance().showOrHide();
					LogSendLogic.getInstance().btnClickNumSend(22103);
					break;
				}
				case _emulateBtn:
				{
					EmulateUI.getInstance().showOrHide();
					break;
				}
				case _statusBtn:
				{
					StatusShopUI.getInstance().showOrHide();
					SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_SHOW_GUILD_DONATE);
//					SacredUI.getInstance().show();
					LogSendLogic.getInstance().btnClickNumSend(22106)
					break;
				}		
					
			}
		}
		
		public function set changePlayerExp(value : int):void
		{
			if( _role.getPlayerExp() <= 0 || _role.getPlayerMaxExp() == 0 )
			{
				_expLine.width =	0;
			}
			else
			{
				_expLine.width =	_expLineWidth * Math.min( 1, ( _role.getPlayerExp() / _role.getPlayerMaxExp() ) );
			}
		}
		//
		public function getStartGlow():void
		{
			//a=new GlowTween(_packBtn);
//			a.startGlowHandler();
		}
		public function getStopGlow():void
		{
			//a=new GlowTween(_packBtn);
//			a.stopGlowHandler();
		}
		
		/**
		 *获得背包按钮的舞台全局位置 
		 * @return 
		 * 
		 */
		public function get PackBtnGlobalPoint():Point
		{
			var packBtnGlobalPoint : Point = _ui.bottomBg.localToGlobal(new Point(_packBtn.x,_packBtn.y));
			return packBtnGlobalPoint;
		}
		private const SAVE_MAX_NUM:int =3;
		private var allWhisperArr:Vector.<FriendVo> =new Vector.<FriendVo>();;
		/**
		 *更新最近联系 
		 * @param name  名字
		 * @param type   1：添加 2 删除
		 * 
		 */		
		public function showWhisperInfo(vo:FriendVo,type:int=1):void
		{
			saveWhisperName(vo,type);
			updateWhisper();
		}
		private function checkHave(name:String):int
		{
			for( var i:String in allWhisperArr)
			{
				if(name == allWhisperArr[int(i)].name)
				{
					return int(i);
				}
			}
			return -1;
		}
		private const ADD:int=1,DELETE:int=2;
		private function saveWhisperName(vo:FriendVo,type:int=1):void
		{
			var index: int =checkHave(vo.name);
			if(index==-1)
			{
				if( allWhisperArr.length <SAVE_MAX_NUM )
				{
					if(type == ADD)
						allWhisperArr.push(vo);
				}
				else
				{
					if(type == ADD)
					{
						allWhisperArr.splice(0,1);
						allWhisperArr.push(vo);
					}
				}
			}
			else
			{
				if(type == DELETE)
				{
					allWhisperArr.splice(index,1);
				}
			}
		}
		private const startPoint:Point = new Point(100,450);
		private function updateWhisper():void
		{
			var mc:MovieClip;
			for(var i:int=0; i<SAVE_MAX_NUM;i++)
			{
				mc = getIcion(i);
				if(!mc)
					continue;
				mc.buttonMode=true;
				if(i<allWhisperArr.length)
				{
					mc.visible=true;
					if(!mc.hasEventListener(MouseEvent.CLICK))
					{
						mc.addEventListener(MouseEvent.CLICK,onClickWhisper);
					}
					mc.x = startPoint.x +(i+1)*120;
					mc.y = startPoint.y ;
					mc[DATA] =allWhisperArr[i];
					mc[TXT_INFO].text = allWhisperArr[i].name;
				}
				else
				{
					mc.visible=false;
					mc[DATA] = mc[TXT_INFO].text = "";
				}
			}
		}
		private function onClickWhisper(e:MouseEvent):void
		{
			var mc:MovieClip = e.currentTarget as MovieClip;
			if(mc && mc[DATA])
			{
				FriendLogic.getInstance().WhispChat(mc[DATA] as FriendVo)
				
			}
		}
		private function getIcion(index:int):MovieClip
		{
			if(!_ui[CION+index])
			{
				_ui[CION+index] = ResManager.newResClass("CustomControl_0246");
				_ui.addChild(_ui[CION+index]);
				if(!(_ui[CION+index] as MovieClip)[TXT_INFO])
				{
					var txt:TextField = new TextField;
					txt.selectable=false;
					txt.mouseEnabled=false;
					txt.x =30;
					txt.y =2;
					txt.height =20;
					txt.width  =80;
					txt.textColor = 0xFFFFFF;
					(_ui[CION+index] as MovieClip)[TXT_INFO] =txt;
					(_ui[CION+index] as MovieClip).addChild(txt)
				}
				
			}
			return (_ui[CION+index] as MovieClip);
			
		}
		
	}
}